package com.dsj.gxrd.modules.book.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsj.gxrd.modules.book.domain.dto.RdCommShareDto;
import com.dsj.gxrd.modules.book.domain.dto.RdFileDto;
import com.dsj.gxrd.modules.book.domain.entity.RdCommShare;
import com.dsj.gxrd.modules.book.domain.query.RdCommShareQuery;
import com.dsj.gxrd.modules.book.domain.vo.RdCommShareVo;
import com.dsj.gxrd.modules.book.enums.ApprovalStatus;
import com.dsj.gxrd.modules.book.enums.FileRelationTypeEnum;
import com.dsj.gxrd.modules.book.mapper.RdCommShareMapper;
import com.dsj.gxrd.modules.book.service.IRdFileService;
import com.dsj.gxrd.modules.book.service.RdCommShareService;
import com.ruoyi.common.core.utils.Assert;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author songpin566
 * @date 2023/09/14
 **/
@Service
public class RdCommShareServiceImpl extends ServiceImpl<RdCommShareMapper, RdCommShare> implements RdCommShareService {

    private IRdFileService rdFileService;

    private RemoteUserService userService;

    public RdCommShareServiceImpl(IRdFileService rdFileService) {
        this.rdFileService = rdFileService;
    }

    @Override
    public Page<RdCommShareVo> selectRdCommonList(RdCommShareQuery share) {
        //模糊搜索同时搜索的是标题以及内容
        IPage<RdCommShare> iPage = new Page<>(share.getPageNum(), share.getPageSize());
        String searchVal = share.getSearchVal();
        String sharePerson = share.getSharePerson();
        LocalDateTime beginTime = share.getBeginTime();
        LocalDateTime endTime = share.getEndTime();
        iPage = this.lambdaQuery()
                .and(
                        StringUtils.isNotEmpty(share.getSearchVal()),
                        (item) -> item
                                .like(RdCommShare::getContent, searchVal)
                                .or()
                                .like(RdCommShare::getTitle, searchVal)
                  )
                .like(StringUtils.isNotEmpty(sharePerson), RdCommShare::getSharePerson, sharePerson)
                .eq(share.getApprovalStatus()!=null,RdCommShare::getApprovalStatus,share.getApprovalStatus())
                .between(beginTime != null && endTime != null, RdCommShare::getCreateTime, beginTime, endTime)
                .page(iPage);
        List<Long> ids = iPage.getRecords().stream().map(RdCommShare::getRdCommShareId).collect(Collectors.toList());
        Map<Long, List<RdFileDto>> map = rdFileService.listFilesMap(ids, FileRelationTypeEnum.BOOK_SWEET_COMM_SHARE_IMAGE);
        return (Page<RdCommShareVo>) iPage.convert(item -> {
            RdCommShareVo shareVo = BeanUtils.copyProperties(item, RdCommShareVo.class);
            List<RdFileDto> list = map.get(shareVo.getRdCommShareId());
            if (list != null && list.size() > 0) {
                RdFileDto rdFileDto = list.get(0);
                shareVo.setFile(rdFileDto);
            }
            return shareVo;
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertRdCommShare(RdCommShareDto commShareDto) {
        RdCommShare rdCommShare = BeanUtils.copyProperties(commShareDto, RdCommShare.class);
        rdCommShare.setDelFlag(0);
        rdCommShare.setApprovalStatus(ApprovalStatus.WAIT_APPROVAL.getCode());
        int i = this.baseMapper.insert(rdCommShare);
        //文件集
        rdFileService.saveFileBatch(Collections.singletonList(commShareDto.getFile()), rdCommShare.getRdCommShareId(), FileRelationTypeEnum.BOOK_SWEET_COMM_SHARE_IMAGE);
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateRdCommShare(RdCommShareDto commShareDto) {
        Assert.isTrue(commShareDto.getRdCommShareId() == null, "交流分享id不能为空");
        rdFileService.updateFileBatch(Collections.singletonList(commShareDto.getFile()), commShareDto.getRdCommShareId(), FileRelationTypeEnum.BOOK_SWEET_COMM_SHARE_IMAGE);
        return this.lambdaUpdate()
                .eq(RdCommShare::getRdCommShareId, commShareDto.getRdCommShareId())
                .set(RdCommShare::getContent, commShareDto.getContent())
                .set(RdCommShare::getTitle, commShareDto.getTitle())
                .set(RdCommShare::getSharePerson, commShareDto.getSharePerson())
                .set(RdCommShare::getUpdateTime, LocalDateTime.now())
                .set(RdCommShare::getUpdateBy, SecurityUtils.getLoginUser().getUserid())
                .update()
                ? 1 : 0;
    }

    @Override
    public int deleteRdCommShareIds(List<Long> rdCommShareIds) {
        rdFileService.deleteFilesByRelationIds(rdCommShareIds, FileRelationTypeEnum.BOOK_SWEET_COMM_SHARE_IMAGE);
        return removeByIds(rdCommShareIds) ? 1 : 0;
    }

    @Override
    public RdCommShareVo selectRdCommShareById(Long rdCommShareId) {
        RdCommShare commShare = getById(rdCommShareId);
        Assert.isTrue(commShare == null, "输入的id不存在");
        RdCommShareVo share =
                BeanUtils.copyProperties(commShare, RdCommShareVo.class);
        List<RdFileDto> list = rdFileService.listFiles(rdCommShareId, FileRelationTypeEnum.BOOK_SWEET_COMM_SHARE_IMAGE);
        if (list != null && list.size() > 0) {
            share.setFile(list.get(0));
        }
        return share;
    }

    @Override
    public int approval(RdCommShareDto rdCommShare) {
        RdCommShare share = getById(rdCommShare.getRdCommShareId());
        Integer approvalStatus = share.getApprovalStatus();
        Assert.isTrue(approvalStatus==ApprovalStatus.APPROVAL_PASS.getCode()||approvalStatus==ApprovalStatus.APPROVAL_NO_PASS.getCode(),"已经审核不能再次审核");
        boolean update = this.lambdaUpdate()
                .eq(RdCommShare::getRdCommShareId, rdCommShare.getRdCommShareId())
                .set(RdCommShare::getApprovalStatus, rdCommShare.getApprovalStatus())
                .set(RdCommShare::getRemark, rdCommShare.getRemark())
                .set(RdCommShare::getApprovalPerson,SecurityUtils.getLoginUser().getUserid())
                .set(RdCommShare::getApprovalTime,LocalDateTime.now())
                .update();
        return update ? 1 : 0;
    }

}
